<template>
  <div class="login_container w-100% h-100vh bg-cover bg-no-repeat">
    <el-row>
      <el-col :span="12" :xs="0"></el-col>
      <el-col :span="12" :xs="24">
        <el-form
          :model="loginForm"
          :rules="rules"
          ref="loginForms"
          class="login_form relative top-30vh w-80% p-40px bg-cover bg-no-repeat"
        >
          <h1 class="text-white text-40px">Hello</h1>
          <h2 class="text-white text-20px my-20px">欢迎来到后台模板</h2>
          <el-form-item prop="username">
            <el-input
              :prefix-icon="User"
              v-model="loginForm.username"
            ></el-input>
          </el-form-item>
          <el-form-item prop="password">
            <el-input
              :prefix-icon="Lock"
              type="password"
              :show-password="true"
              v-model="loginForm.password"
            ></el-input>
          </el-form-item>
          <el-form-item>
            <el-button
              class="w-100%"
              type="primary"
              size="default"
              :loading="btnLoading"
              @click="login"
            >
              登录
            </el-button>
          </el-form-item>
        </el-form>
      </el-col>
    </el-row>
  </div>
</template>

<script setup lang="ts">
import { reactive, ref } from 'vue'
import { User, Lock } from '@element-plus/icons-vue'
import useUserStore from '@/store/modules/user'
import { useRouter, useRoute } from 'vue-router'
import { ElNotification } from 'element-plus'
import { getTime } from '@/utils/time'

let $router = useRouter()
const $route = useRoute()
let useStore = useUserStore()

let loginForm = reactive({
  username: 'demo_01',
  password: '123456',
})

const btnLoading = ref(false)
const loginForms = ref()

const rules = {
  username: [
    { required: true, message: '用户名不能为空', trigger: 'blur' },
    {
      required: true,
      min: 5,
      max: 10,
      message: '账号长度5-10位',
      trigger: 'change',
    },
  ],
  password: [
    { required: true, message: '密码不能为空', trigger: 'blur' },
    {
      required: true,
      min: 6,
      max: 15,
      message: '密码长度6-15位',
      trigger: 'change',
    },
  ],
}

const login = async () => {
  btnLoading.value = true
  try {
    await useStore.userLogin(loginForm)
    btnLoading.value = false
    // let redirect: any = $route.query.redirect
    // $router.push({ path: redirect || '/' })
    $router.push('/home')
    ElNotification({
      type: 'success',
      message: '欢迎回来',
      title: `HI, ${getTime()}好`,
    })
  } catch (error) {
    
    ElNotification({
      type: 'error',
      message: (error as Error).message,
    })
    btnLoading.value = false
  }
}
</script>

<style scoped lang="scss">
.login_container {
  background: url('@/assets/images/background.jpg') no-repeat;
  .login_form {
    background: url('@/assets/images/login_form.png') no-repeat;
  }
}
</style>
